package admin

import (
	"context"

	"suci-go-zero/common/xerr"
	"suci-go-zero/internal/model"
	"suci-go-zero/internal/svc"
	"suci-go-zero/internal/types"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type RoleDelLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleDelLogic {
	return &RoleDelLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleDelLogic) RoleDel(req *types.Ids) error {
	err := l.svcCtx.Models.Role.Trans(l.ctx, func(context context.Context, session sqlx.Session) error {
		for _, roleId := range req.Ids {
			_, err := l.svcCtx.Models.Role.FindOne(l.ctx, int64(roleId))
			if err != nil {
				return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找失败%v", err)
			}
			if err == model.ErrNotFound {
				continue
			}
			err = l.svcCtx.Models.Role.Delete(l.ctx, session, int64(roleId))
			if err != nil {
				return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除校色失败%v", err)
			}

		}
		return nil
	})
	//

	return err
}
